package com.rzjm.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.PlatformPayMapper;
import com.rzjm.api.dao.PropertyMapper;
import com.rzjm.api.entity.PlatformPay;
import com.rzjm.api.entity.Property;
import com.rzjm.api.pay.nongshang.NSAppletOrders;
import com.rzjm.api.pay.nongshang.NSQuery;
import com.rzjm.api.pay.nongshang.NSRefund;
import com.rzjm.api.service.PlatformPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 银行支付列Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@Service
public class PlatformPayServiceImpl extends BaseServiceImpl<PlatformPayMapper, PlatformPay> implements PlatformPayService {

    @Resource
    private PlatformPayMapper platformPayMapper;
    @Resource
    private PropertyMapper propertyMapper;

    @Value("${web.appid}")
    private String appid;

    @Value("${web.nsh_notify_url}")
    private String nsh_notify_url;


    /**
     *  农商行的  微信下单
     * @param out_trade_no
     * @param totalAmount
     * @param subject
     * @param open_id
     * @param attach
     * @param platformPay
     * @return
     */
    @Override
    public String appletOrder(String out_trade_no, BigDecimal totalAmount, String subject, String open_id, String attach,PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSAppletOrders.addOrder(out_trade_no,platformPay,appid,open_id,totalAmount,attach,nsh_notify_url,subject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }

    @Override
    public String orderQuery(String out_trade_no, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSQuery.queryOrder(out_trade_no,platformPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    /**
     * 退款
     * @param out_trade_no
     * @param refund_money
     * @param out_refund_no
     * @param platformPay
     * @return
     */
    @Override
    public String refundOrder(String out_trade_no, BigDecimal refund_money, String out_refund_no,PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSRefund.refundOrder(out_trade_no,refund_money,out_refund_no,platformPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }

    @Override
    public String refundOrderQuery(String out_trade_no, PlatformPay platformPay) {
        String responseData = null;
        try {
            responseData = NSQuery.queryOrder(out_trade_no,platformPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    @Override
    public PlatformPay getDefaultAccount() {
        QueryWrapper<PlatformPay> qw = new QueryWrapper<>();
        qw.eq("is_default",1);
        qw.last("limit 1");
        PlatformPay platformPay = platformPayMapper.selectOne(qw);
        return platformPay;
    }

    @Override
    public PlatformPay getAccountByPropertyId(int propertyId) {
        PlatformPay platformPay = null;
        Property property = propertyMapper.selectById(propertyId);
        if(property!=null){
            platformPay = platformPayMapper.selectById(property.getPayPlatformId());
        }
        return platformPay;
    }
}
